<template>
	<view class="weather-page">
		<button @click="fetchWeather">获取今天天气预报</button>
		<view class="weather-info" v-if="weatherData">
			<view>日期：{{ weatherData.date }}</view>
			<view>星期：{{ weatherData.week }}</view>
			<view>最高温：{{ weatherData.high }}°C</view>
			<view>最低温：{{ weatherData.low }}°C</view>
		</view>
		<view class="loading" v-else>加载中...</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				weatherData: null
			};
		},
		methods: {
			fetchWeather() {
				uni.request({
					url: "http://t.weather.sojson.com/api/weather/city/101230501",
					method: "GET",
					success: (res) => {
						if (res.statusCode === 200 && res.data.status === 200) {
							this.weatherData = res.data.data.forecast[0];
						} else {
							uni.showToast({
								title: "获取天气失败",
								icon: "none"
							});
						}
					},
					fail: (err) => {
						console.error("请求失败", err);
						uni.showToast({
							title: "网络异常",
							icon: "none"
						});
					}
				});
			}
		}
	};
</script>

<style scoped>
	.weather-page {
		padding: 30rpx;
	}

	.weather-info {
		margin-top: 20rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		line-height: 1.8;
	}

	.loading {
		margin-top: 20rpx;
		color: #999;
	}
</style>